package com.quanquan.management.service;

import DTO.PageDTO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.IntegralTradingDetailsVO;
import com.quanquan.management.request.IntegralDetailsRequestBody;
import entity.ExcelData;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.DateUtils;
import utils.ExcelUtil;
import utils.Request;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.util.List;

/**
 * 功能描述:积分后台页面管理
 *
 * @ClassName: IntegralTradingService
 * @Author: xiaoqianh丶
 * @Date: 2019-03-29 14:09
 * @Version: V1.0
 */
@Service
public class IntegralTradingService {


    @Value("${module-url.userUrl}")
    private String userUrl;

    public PageDTO<IntegralTradingDetailsVO> listIntegralTrading(IntegralDetailsRequestBody requestBody) {
        String json = Request.postJson(userUrl + "integral_details/query_integral_list", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        PageDTO<IntegralTradingDetailsVO> pageDTO = new PageDTO<>();
        if (null == jsonObject) {
            return pageDTO;
        }
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            JSONObject jsonData = jsonObject.getJSONObject("data");
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            JSONArray list = jsonData.getJSONArray("list");
            List<IntegralTradingDetailsVO> vos = new LinkedList<>();
            for (int i = 0; i < list.size(); i++) {
                IntegralTradingDetailsVO object = list.getObject(i, IntegralTradingDetailsVO.class);
                vos.add(object);
            }
            data.setList(vos);
            return data;
        }
        return pageDTO;
    }

    public boolean record(IntegralDetailsRequestBody requestBody) {
        String json = Request.postJson(userUrl + "integral_details/record", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        return resCode == 0;
    }

    public void export(HttpServletResponse response, IntegralDetailsRequestBody requestBody) throws Exception {
        List<String> titles = new LinkedList<>();
        titles.add("会员卡号");
        titles.add("会员手机号");
        titles.add("用户名");
        titles.add("渠道");
        titles.add("交易动作");
        titles.add("积分数");
        titles.add("交易说明");
        titles.add("生成时间");
        titles.add("商户名称");
        titles.add("商户业态");
        titles.add("订单号");
        ExcelData data = ExcelUtil.getExcelData();
        data.setTitles(titles);
        PageDTO<IntegralTradingDetailsVO> pageDTO = this.listIntegralTrading(requestBody);
        List<IntegralTradingDetailsVO> list = pageDTO.getList();
        List<List<Object>> rows = new LinkedList<>();
        list.forEach(vo -> {
            List<Object> row = new LinkedList<>();
            row.add(vo.getMemberCardNum());
            row.add(vo.getPhone());
            row.add(vo.getMemberName());
            if (vo.getConsumeType().equals("parking")) {
                row.add("停车场");
            } else if (vo.getConsumeType().equals("admin")) {
                row.add("管理员");
            } else if (vo.getConsumeType().equals("user")) {
                row.add("签到");
            } else if (vo.getConsumeType().equals("selfHelp")) {
                row.add("自助积分");
            } else {
                row.add("优惠券兑换");
            }

            row.add(vo.getTradeAction() == 0 ? "收入" : "扣除");
            row.add(vo.getIntegralPoint());
            row.add(vo.getTradingContent());
            row.add(vo.getCreateTime());
            row.add(vo.getStoreName());
            row.add(vo.getStoreCommercial());
            row.add(vo.getOrderNum());
            rows.add(row);
        });
        data.setRows(rows);
        String now = DateUtils.currentDate(DateUtils.DATE_HOUR_SECOND);
        ExcelUtil.exportExcel(response, "积分记录" + now, data);
    }
}
